<?php

namespace app\admin\controller;

use app\common\controller\Send;
use app\admin\model\OauthIntegral AS IntegralModel;

class OauthIntegral extends Base
{
    use Send;

    public function index()
    {
        $search = input('');
        $oauthId = isset($search['oauth_id']) ? $search['oauth_id'] : 0;
        $nickname = isset($search['nickname']) ? $search['nickname'] : '';
        $startTime = isset($search['start_time']) ? $search['start_time'] : '';
        $endTime = isset($search['end_time']) ? $search['end_time'] : '';
        $type = isset($search['type']) ? $search['type'] : -1;
        $search = [
            'type' => $type,
            'oauth_id' => $oauthId,
            'nickname' => $nickname,
            'start_time' => $startTime,
            'end_time' => $endTime
        ];

        $where = [];
        if ($search['nickname']) {
            $where[] = ['nickname', 'like', '%' . $search['nickname'] . '%'];
        }

        if ($search['type'] != null && $search['type'] != -1) {
            $where[] = ['type', '=', $search['type']];
        }

        if ($search['start_time']) {
            $where[] = ['create_time', '>=', strtotime($search['start_time'])];
        }

        if ($search['end_time']) {
            $where[] = ['create_time', '<=', strtotime($search['end_time'])];
        }
        $model = IntegralModel::getList($search['oauth_id'], $where);
        return view('index', [
            'sum' => $model['sum'],
            'rest' => $model['rest'],
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'type' => IntegralModel::attributes('type'),
            'cancelUrl' => url('index', ['oauth_id' => $search['oauth_id']])
        ]);
    }
}
